package com.fit4as.fit
{
import com.fit4as.util.iterator.Iterator;
import com.fit4as.util.iterator.ArrayIterator;


    // Copyright (c) 2002 Cunningham & Cunningham, Inc.
    // Released under the terms of the GNU General Public License version 2 or later.


public class Summary extends Fixture
{
    public static var countsKey : String = "counts";

    override public function doTable(table : Parse) : void {
        summary.put(countsKey, counts.toString());
        var keys : Array = summary.keySet().sort();
        table.parts.more = rows(new ArrayIterator(keys));
    }

    protected function rows(keys : Iterator) : Parse {
        if (keys.hasNext()) {
            var key : String = String(keys.next());
            trace("Summary#rows " + [ key, summary.get(key)]);
            var result : Parse =
                    tr(
                            td(key.toString(),
                                    td(summary.get(key).toString(),
                                            null)),
                            rows(keys));

            if (key == countsKey) mark(result);
            return result;
        }
        return null;
    }

    protected function tr(parts : Parse, more : Parse) : Parse {
        return Parse.createParseFromStrings("tr", null, parts, more);
    }

    protected function td(body : String, more : Parse) : Parse {
        return Parse.createParseFromStrings("td", info(body), null, more);
    }

    protected function mark(row : Parse) : void {
        // mark summary good/bad without counting beyond here
        var official : Counts = counts;
        counts = new Counts();
        var cell : Parse = row.parts.more;
        if (official.wrong + official.exceptions > 0) {
            wrong(cell);
        } else {
            right(cell);
        }
        counts = official;
    }

}
}